#include <QDir>
#include <QFont>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTranslator>
#include <QStandardPaths>
#include <QSettings>
#include <QDebug>
#include "MainApp.h"
#include "MYPlayer.h"
#include "MYVideoOutput.h"

#define APP_NAME                                "JLPlayer"
#define APP_DISPLAYNAME                         "JLPlayer"
#define APP_ICON_PATH                           ":/image/AppIcon.ico"
#define UI_MAIN_PATH                            "qrc:/qml/main.qml"
#define UI_TRANSLATION_PATH                     ":/translation/MYPlayer_zh_CN.qm"
#define IMAGEDIR_RELATIVE_PATH                  "./resource/ui/image"
#define DEFAULT_FONT_NAME                       "\xE5\xBE\xAE\xE8\xBD\xAF\xE9\x9B\x85\xE9\xBB\x91"// 微软雅黑

MainApp* g_app = nullptr;

MainApp* GetMainApp()
{
    return g_app;
}

QObject* MainApp::GetInstance(QQmlEngine* engine, QJSEngine* scriptEngine)
{
    QObject* obj = GetMainApp();
    // 当 QML 通过声明的方法从 C++ 获取一个 QObject 对象的指针时，QML 将会获取这个 QObject 对象的所有权
    // 除非显式调用 QQmlEngine::setObjectOwnership() 方法并指定为 QQmlEngine::CppOwnership
    // 避免obj在qml中被析构掉
    QQmlEngine::setObjectOwnership(obj, QQmlEngine::CppOwnership);
    return obj;
}

MainApp::MainApp(int &argc, char **argv)
    : QApplication(argc, argv)
    , m_strAppRootPath()
    , m_strImageRootPath()
{
}

MainApp::~MainApp() = default;

int MainApp::Main(int argc, char *argv[])
{
    // 初始化失败后，反初始化
    if(!Initialize())
    {
        Uninitialize();
        return EXIT_FAILURE;
    }
    // 执行程序
    int ret = exec();
    
    // 反初始化
    Uninitialize();
    
    return ret;
}

void MainApp::testFunc()
{
    setDemoNum(demoNum() + 1);
}

void MainApp::changeLanguage(QString language)
{
    if(language == MainApp::language()) return;
    m_pTranslator.reset(new QTranslator);
    QSettings* settingIni = new QSettings("setting.ini", QSettings::IniFormat);
    settingIni->setValue("Config/Language", language);
    if(language == "Chinese")
    {
        if(!m_pTranslator->load(QStringLiteral(UI_TRANSLATION_PATH))) return;
        installTranslator(m_pTranslator.data());
    }
    if(language == "English")
    {
        installTranslator(m_pTranslator.data());
    }
    setLanguage(language);
    delete settingIni;
}

// demoNum
int MainApp::demoNum() const
{
    return m_mDemoNum;
}

void MainApp::setDemoNum(int newValue)
{
    if (m_mDemoNum != newValue)
    {
        m_mDemoNum = newValue;
        emit demoNumChanged(m_mDemoNum);
    }
}


QString MainApp::language() const
{
    return m_mLanguage;
}

void MainApp::setLanguage(QString newValue)
{
    if(m_mLanguage != newValue)
    {
        m_mLanguage = newValue;
        emit languageChanged(m_mLanguage);
    }
}

bool MainApp::Initialize()
{
    // 设置应用程序属性
    setApplicationName(QStringLiteral(APP_NAME));
    setWindowIcon(QIcon(QStringLiteral(APP_ICON_PATH)));
    setApplicationDisplayName(tr(APP_DISPLAYNAME));
    
    InitializePath();
    InitializeCfg();
    if(!InitializeTranslator()) return false;
    if(!InitializeQmlEngine()) return false;
    if(!InitializeUI(m_pQmlEngine.data())) return false;
    InitializeOthers();
    
    return true;
}

// 反初始化与初始化顺序反着来
bool MainApp::Uninitialize()
{
    UninitializeOthers();
    UninitializeUI(m_pQmlEngine.data());
    UninitializeQmlEngine();
    UninitializeTranslator();
    UninitializeCfg();
    UninitializePath();

    return true;
}


bool MainApp::InitializePath()
{
    // qApp 是qapplication中的一个全局指针，指向一个全局的qApplication对象
    m_strAppRootPath = qApp->applicationDirPath();
    QDir dirApp(m_strAppRootPath);
    if(!dirApp.exists()) return false;

    m_strImageRootPath = m_strAppRootPath;
    m_strImageRootPath.append(QStringLiteral("/")).append(QStringLiteral(IMAGEDIR_RELATIVE_PATH));
    QDir dirImg(m_strImageRootPath);
    if(!dirImg.exists()) {}
    m_strImageRootPath = dirImg.canonicalPath();

    return true;
}

bool MainApp::UninitializePath()
{
    m_strImageRootPath.clear();
    m_strAppRootPath.clear();

    return true;
}

bool MainApp::InitializeCfg()
{
    return true;
}

bool MainApp::UninitializeCfg()
{
    return true;
}

bool MainApp::InitializeTranslator()
{
    if(m_pTranslator != nullptr) return true;
    m_pTranslator.reset(new QTranslator);
    QSettings *settingIni = new QSettings("setting.ini", QSettings::IniFormat);

    QString language = settingIni->value("Config/Language").toString();
    qDebug() << language << endl;

    if(language == NULL)
    {
        settingIni->setValue("Config/Language", QString("Chinese"));
        language = settingIni->value("Config/Language").toString();
    }
    setLanguage(language);
    if(language == "Chinese")
    {
        if(!m_pTranslator->load(QStringLiteral(UI_TRANSLATION_PATH)))
        { return false; }
        installTranslator(m_pTranslator.data());
    }
    if(language == "English")
    {

    }
    delete settingIni;
    return true;
}

bool MainApp::UninitializeTranslator()
{
    if(!m_pTranslator) return true;
    removeTranslator(m_pTranslator.data());
    m_pTranslator.reset();
    return true;
}

bool MainApp::InitializeQmlEngine()
{
    if (m_pQmlEngine != nullptr) return true;
    m_pQmlEngine.reset(new QQmlApplicationEngine);
    return (nullptr != m_pQmlEngine);
}

bool MainApp::UninitializeQmlEngine()
{
    if (!m_pQmlEngine) return true;
    m_pQmlEngine.reset();
    return true;
}

bool MainApp::InitializeUI(QQmlApplicationEngine* pQmlEngine)
{
    if (pQmlEngine == nullptr) return false;

    // 设置程序默认字体
    setFont(QFont(DEFAULT_FONT_NAME, 9));

    // Register Qml Types
    qmlRegisterSingletonType<MainApp>("com.julin.myplayer", 1, 0, "MainApp",
                                      &MainApp::GetInstance);
    qmlRegisterType<MYPlayer>("com.julin.myplayer", 1, 0, "JLPlay");
    qmlRegisterType<MYVideoOutput>("com.julin.myplayer", 1, 0, "MYVideoOutput");


//    qmlRegisterType<MYQmlVideoOutput>("com.julin.myplayer", 1, 0, "MYQmlVideoOutput");

    // Set context property
//    pQmlEngine->rootContext()->setContextProperty("appCfg", m_pCfg.data());

    // Add image provider
//    pQmlEngine->addImageProvider(LMCBtnImageProvider::s_strProviderId,
//                            new LMCBtnImageProvider(m_strImageRootPath));
//    pQmlEngine->addImageProvider(LMCImageProvider::s_strProviderId,
//                            new LMCImageProvider(m_strImageRootPath));

    // Load main UI qml from resource
    pQmlEngine->load(QUrl(QStringLiteral(UI_MAIN_PATH)));
    QList<QObject*> rootObjs(pQmlEngine->rootObjects());
    return !rootObjs.isEmpty();;
}

bool MainApp::UninitializeUI(QQmlApplicationEngine* pQmlEngine)
{
    if (pQmlEngine == nullptr) return true;
    return true;
}

bool MainApp::InitializeOthers()
{
    // 注册类型
    //qRegisterMetaType<QSharedPointer<WorkTask>>();
    return true;
}

bool MainApp::UninitializeOthers()
{
    return true;
}
